<template>
  <a-modal
    :visible="visible"
    :title="state.title"
    :width="700"
    @cancel="handleCancel"
    :footer="null"
  >
    <a-table
      style="min-height: 50vh"
      :columns="state.columns"
      :data-source="state.data"
      :pagination="{ pageSize: 5 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a-button @click="onClick(record)">查看详情</a-button>
        </template>
      </template>
    </a-table>
  </a-modal>
  <InfoModal
    :visible="isInfoModalVisible"
    :data="modalData"
    :title="'详情查看'"
    @update:visible="isInfoModalVisible = $event"
  />
</template>

<script setup>
import { defineProps, defineEmits, reactive, onMounted, ref } from "vue";
import { alovaInstance } from "/@/utils/request.js";
import { convertDateString } from "/@/utils/tool.js";
import {Modal,Table} from 'ant-design-vue'
import InfoModal from "./InfoModal.vue"
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  modelData: {
    type: Object,
    default: "",
  },
});
const isInfoModalVisible = ref(false);
const modalData = ref([]);
const state = reactive({
  data: [],
  columns: [
    {
      title: "位置",
      dataIndex: "address",
      key: "address",
    },
    {
      title: "时间",
      dataIndex: "date",
      key: "date",
    },
    {
      title: "详情",
      dataIndex: "detail",
      key: "detail",
    },
    {
      title: "操作",
      width: 120,
      key: "action",
    },
  ],
  title: "详情查看",
});
const emit = defineEmits(["update:visible"]);
const onClick = (item) => {
  isInfoModalVisible.value = true;
  if (props.modelData.modelType === "Substation") {
    goToListPageBj(item);
  } else {
    handleOpenCarModal(item);
  }
};
onMounted(() => {
  getData(props.modelData);
});
const getData = async () => {
  if (props.modelData.modelType === "Substation") {
    getListFaultTop();
  } else {
    getListWarnTop();
  }
};
const getListWarnTop = async () => {
  const res = await alovaInstance.Get(
    "/Portal/TYHome.ashx?Method=GetJCWDefectBySiteCode",
    {
      params: {
        deptCode: props.modelData.value,
      },
    }
  );
  if (res && res.data.length > 0) {
    state.data = res.data.map((item) => {
      const anchoringCode = item.Device.AnchoringCode?`(${item.Device.AnchoringCode})`:' '
      return {
        address: `${item.Device.Devicecode}${anchoringCode}${item.Site.Sitename}`,
        date: convertDateString(item.Discoverytime),
        detail: item.Defecttype.Defectname,
        ...item,
      };
    });
  }
  state.title = props.modelData.value + " 接触网缺陷列表";
};

const getListFaultTop = async () => {
  const res = await alovaInstance.Get(
    "/Portal/TYHome.ashx?Method=GetBDDefectBySubstationName",
    {
      params: {
        substationName: props.modelData.value,
      },
    }
  );
  if (res && res.data.length > 0) {
    state.data = res.data.map((item) => {
      return {
        address: `${item.Device.Devicename}(${item.Device.Substation.Substationname})`,
        date: convertDateString(item.Discoverytime),
        detail: item.Defecttype.Defectname,
        ...item,
      };
    });
  }
  state.title = props.modelData.value + " 变电所缺陷列表";
};
const handleOpenCarModal = (item) => {
  modalData.value = [
    { label: "工区", value: item.Department.Deptname },
    { label: "区站", value: item.Site.Sitename },
    { label: "支柱号", value: item.Device.Devicecode },
    { label: "缺陷级别", value: item.Grade },
    { label: "缺陷分类", value: item.Defecttype.Defectname },
    { label: "创建时间", value: convertDateString(item.Createtime) },
    { label: "发现人", value: item.Discoverer },
    { label: "发现时间", value: convertDateString(item.Discoverytime) },
  ];
  isInfoModalVisible.value = true;
};
const goToListPageBj = (item) => {
  modalData.value = [
    { label: "变电所", value: item.Substation.Substationname },
    { label: "缺陷级别", value: item.Grade },
    { label: "缺陷分类", value: item.Defecttype.Defectname },
    { label: "创建时间", value: convertDateString(item.Createtime) },
    { label: "发现人", value: item.Discoverer },
    { label: "发现时间", value: convertDateString(item.Discoverytime) },
    { label: "缺陷描述", value: item.Device.Defectdeseription },
  ];
  isInfoModalVisible.value = true;
};
const handleCancel = () => {
  emit("update:visible", false);
};
</script>
